﻿clear all
set more off
set maxvar 20000

*Define directory with input data
cd "Q:"

*Create dataset with selected variables

*Create extract from 2020 V2 RAND HRS Detailed Imputations File
use "randhrsimp1992_2020v2.dta", clear

*Keep relevant variables - Respondent’s wage/salary income, bonuses/overtime pay/commissions/tips, 2nd job or military reserve earnings, and professional practice or trade income
local constant = "hhid pn hhidpn inw*"
local workinc = "r*iwage r*ibon r*i2nd r*itrad r*iearn r*iosemp r*isemp"
local hhinc = "h*itot h*iotot h*iftot h*hhidc h*idivin h*ibndin h*istk1 h*istk2 h*ichkin h*icdin"
local stocks = "r*istk1 r*istk2 r*istk3"
local flag = "r*ifwage r*ifbon r*if2nd r*iftrad r*ifearn r*ifsemp"
keep `constant' `workinc' `hhinc' `stocks' `flag'

*Define number of waves
local maxwave=15
   
*Wave-specifc variables
local vars = "iwage ibon i2nd itrad iearn ifwage ifbon if2nd iftrad ifearn"
local hhvars = "itot iotot iftot hhidc idivin"
local hhvars3 = "ibndin ichkin icdin"
local se_vars = "iosemp isemp ifsemp"

*Individual
forvalues i=1/`maxwave' {
foreach v in `vars' {
	gen `v'`i'=r`i'`v' if inw`i'==1
}
}

*Household
forvalues i=1/`maxwave' {
foreach v in `hhvars' {
	gen `v'`i'=h`i'`v' if inw`i'==1
}
}

*Household variables beginning in wave 3
forvalues i=3/`maxwave' {
foreach v in `hhvars3' {
	gen `v'`i'=h`i'`v' if inw`i'==1
}
}

*Individual variables beginning in wave 3
forvalues i=3/`maxwave' {
foreach v in `se_vars' {
	gen `v'`i'=r`i'`v' if inw`i'==1
}
}

*Reshape data
keep hhid pn hhidpn i*
quietly des *11, varlist
local list=subinstr("`r(varlist)'","11","",.)
disp "`list'"
reshape long "`list'", i(hhidpn) j(year) 
   
rename year wave

gen waveyr=1992+(wave-1)*2

save hrsrandimpdata_1994_2020, replace
